package top.infopub.mgr.common.vo;


import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import top.infopub.mgr.common.vo.ErrorCodeEnum;


/**
 * RspVo mgr 内部使用  用于layui 定制化显示
 * @author Awoke
 * @version 2018年9月13日
 * @see RspVo
 * @since
 */
public class RspVo<T> implements Serializable {

    private static final long serialVersionUID = -8073528029447015345L;

    /**
     * 是否成功标志。
     * 需要特别说明的是，如果返回处理中，则此处为false
     */
    private boolean isSuccess;

    /**
     * 错误编码。参考{@link ErrorCodeEnum}
     * （命名必须为code 对应layui数据格式）
     */
    private int code;

    /**
     * 错误返回说明
     */
    private String msg;

    /**
     * 返回的实体类
     */
    private T entity;

    /**
     * 记录总条数
     */
    private long count = 0;
    
    /**
     * 总页数
     */
    private int pages;

    /**
     * 结果集存放List
     */
    private List<T> data = new LinkedList<>();

    /**
     * isSuccess=false
     * @param data
     */
    public RspVo() {
        super();
        this.isSuccess = false;
    }
    
    /**
     * isSuccess=true
     * @param data
     */
    public RspVo(T entity) {
        super();
        this.isSuccess = true;
        this.code = 0;
        this.entity = entity;
    }

    /**
     * isSuccess=true
     * @param count
     * @param entity
     */
    public RspVo(long count, T entity) {
        super();
        this.isSuccess = true;
        this.code = 0;
        this.entity = entity;
        this.count = count;
    }

    /**
     * isSuccess=true
     * @param count
     * @param data
     */
    public RspVo(long count, List<T> data) {
        super();
        this.isSuccess = true;
        this.code = 0;
        this.count = count;
        this.data = data;
    }
    
    /**
     * isSuccess=true
     * @param pages
     * @param count
     * @param data
     */
    public RspVo(int pages, long count, List<T> data) {
        super();
        this.pages = pages;
        this.isSuccess = true;
        this.code = 0;
        this.count = count;
        this.data = data;
    }

    public RspVo(boolean isSuccess, String msg) {
        super();
        this.isSuccess = isSuccess;
        this.msg = msg;
    }
    
    public RspVo(boolean isSuccess, int code, String msg) {
        super();
        this.isSuccess = isSuccess;
        this.code = code;
        this.msg = msg;
    }

    public RspVo(boolean isSuccess, int code, String msg, T entity) {
        super();
        this.isSuccess = isSuccess;
        this.code = code;
        this.msg = msg;
        this.entity = entity;
    }

    public static <T> RspVo<T> getSuccessResponse() {
        return new RspVo<T>(true, 0, null);
    }

    public static <T> RspVo<T> getSuccessResponse(T entity) {
        return new RspVo<T>(entity);
    }
    
    public static <T> RspVo<T> getSuccessResponse(int code, String msg, T entity) {
    	return new RspVo<T>(true, code, msg, entity);
    }

    public static <T> RspVo<T> getSuccessResponse(int code, String msg) {
        return new RspVo<T>(true, code, msg);
    }

    /** Success 同时设置msg **/
    public static <T> RspVo<T> getSuccessMsgResponse(String msg) {
        RspVo<T> rsp = new RspVo<T>(null);
        rsp.setMsg(msg);
        return rsp;
    }

    public static <T> RspVo<T> getSuccessResponse(long count, T entity) {
        return new RspVo<T>(count, entity);
    }

    public static <T> RspVo<T> getSuccessResponse(long count, List<T> data) {
        return new RspVo<T>(count, data);
    }
    
    public static <T> RspVo<T> getSuccessResponse(int pages, long count, List<T> data) {
        return new RspVo<T>(pages, count, data);
    }
    
    public static <T> RspVo<T> getFailureResponse(Integer code, String msg) {
    	return new RspVo<T>(false, code, msg);
    }

    public RspVo<T> setDataObj(T entity) {
        this.setEntity(entity);
        return this;
    }

    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    public boolean isSuccess() {
        return isSuccess;
    }

    public void setSuccess(boolean isSuccess) {
        this.isSuccess = isSuccess;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }

    public int getPages() {
        return pages;
    }

    public void setPages(int pages) {
        this.pages = pages;
    }
}
